<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .form-container {
            width: 400px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .form-container input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border-radius: 5px;
            border: 1px solid #ccc;
        }
        .form-container button {
            width: 48%;
            padding: 10px;
            margin-top: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .form-container button:hover {
            background-color: #45a049;
        }
        .form-container .reset-btn {
            background-color: #f44336;
        }
        .form-container .reset-btn:hover {
            background-color: #e53935;
        }
    </style>
</head>
<body>

<div class="form-container">
    <h2>修改密码</h2>
    <form id="updatePwdForm" onsubmit="event.preventDefault(); submitForm();">
        <label for="name">用户名</label>
        <input type="text" id="name" name="name" required>

        <label for="newPassword">新密码</label>
        <input type="password" id="newPassword" name="newPassword" required>

        <button type="submit">提交</button>
        <button type="button" class="reset-btn" onclick="resetForm()">重置</button>
    </form>
</div>

<script>
    function submitForm() {
        var name = document.getElementById('name').value;
        var newPassword = document.getElementById('newPassword').value;

        var formData = new FormData();
        formData.append("name", name);
        formData.append("newPassword", newPassword);

        $.ajax({
            url: '/card/updatepwd',  // 后端接口地址
            type: 'POST',
            data: formData,           // 传递 FormData
            processData: false,       // 防止 jQuery 自动处理数据
            contentType: false,       // 不设置 contentType，使用默认的 multipart/form-data
            success: function (response) {
                alert('密码修改成功');
                window.location.href = '/card/main';  // 跳转到主页面
            },
            error: function (xhr, status, error) {
                alert('密码修改失败: ' + error);
            }
        });
    }

    function resetForm() {
        document.getElementById('updatePwdForm').reset();
    }
</script>

</body>
</html>
